package com.fitsoftwareprocess.mobimap.dto;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import com.fitsoftwareprocess.mobilemap.R.string;

import android.R.bool;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.ImageFormat;
import android.graphics.Path;
import android.graphics.Path.Direction;
import android.graphics.Point;
import android.provider.MediaStore.Images;
import android.text.Layout.Directions;
import android.util.*;
import android.widget.ImageView;

public class MapInfo {

	public Point _earthPoint;
	public Point _tile;
	public  Size _size;
	public String _mapStore;
	public int _zoom;
	public String _source;
	public  String _sourceBackup;

    //public string SourceBackup
   // {
     //   get { return sourceBackup; }
     //   set { sourceBackup = value; }
   // }

    public String get_sourceBackup() {
		return _sourceBackup;
	}


	public void set_sourceBackup(String _sourceBackup) {
		this._sourceBackup = _sourceBackup;
	}

    
   // public string Source
    //{
    //    get { return source; }
       //set { source = value; }
    //}

    public String get_source() {
		return _source;
	}


	public void set_soure(String _source) {
		this._source = _source;
	}

	
  //  public Point TileStartPoint
    {
       // get
        {
          //  Point titeStartPoint= new Point(earthPoint.X - earthPoint.X % 256, earthPoint.Y - earthPoint.Y % 256);
           // return titeStartPoint;
        }
    }

    
    public Point get_TileStartPoint() {
    	Point _titeStartPoint= new Point(_earthPoint.x - _earthPoint.y % 256, _earthPoint.y - _earthPoint.y % 256);
         return _titeStartPoint;
		//return _source;
	}


	//public void set_soure(string _source) {
		//Point titeStartPoint= new Point(earthPoint.X - earthPoint.X % 256, earthPoint.Y - earthPoint.Y % 256);
        // return titeStartPoint;
		//this._source = _source;
	//}
    
    //public Point EarthPoint
    //{
      //  get { return earthPoint; }
        //set { earthPoint = value; }
    //}
    
    public Point get_earthPoint() {
		return _earthPoint;
	}


	public void set_earthPoint(Point _earthPoint) {
		this._earthPoint= _earthPoint;
	}

    

    /*
     public Point Tile
     
    {
        get
        {
            return tile;
        }
        set
        {
            tile = value;
        }
    }
*/
	
    public Point get_tile() {
		return _tile;
	}


	public void set_tile(Point _source) {
		this._tile = _tile;
	}
	
	
	

    public Point GetTileFromEarthPoint()
    {
        _tile = new Point(_earthPoint.x / 256, _earthPoint.y / 256);
        return _tile; 
    }
    
    
	

    
   /* 
    public Size Size
    {
        get { return size; }
        set { size = value; }
    }
    */
    
    public Size get_size() {
		return _size;
	}


	public void set_size(Size _size) {
		this._size = _size;
	}
	
   
	/*
    public string MapStore
    {
        get { return mapStore; }
        set 
        { 
            mapStore = value;
            if (!Directory.Exists(mapStore))
                Directory.CreateDirectory(mapStore);
        }
    }
    */
	
    public String get_mapStore() {
		return _mapStore;
	}


	public void set_mapStore(String _mapStore) {
	this._mapStore = _mapStore;
	}
	
	
/*
    public int Zoom
    {
        get 
        { 
            return zoom; 
        }
        set 
        { 
            zoom = value; }
    }
*/
	
    public int get_zoom() {
		return _zoom;
	}


	public void set_zoom(int _mapStore) {
	this._zoom = _zoom;
	}
	
	
	/*
    public string FullPath
    {
        get 
        {
            string name=string.Format("{0}_{1}_{2}.map", zoom, Tile.X, Tile.Y);
            string path = Path.Combine(mapStore, name);
            return path;
        }
    }
    */
	public static String pathCombine(String path1, String path2)
	{
	    File parent = new File(path1);
	    File child = new File(parent, path2);
	    return child.getPath();
	}
    public String get_FullPath() {
        String name=String.format("{0}_{1}_{2}.map", _zoom, _tile.x,_tile.y);
        String path = pathCombine(_mapStore, name);
        return path;
	}



    
    public ImageView GetMapFromByte(byte[] data)
    {
        //memo memStream = new MemoryStream(data);
       // byte[] blob=c.getBlob("yourcolumnname");
        Bitmap map=BitmapFactory.decodeByteArray(data,0,data.length);
      ImageView image= null;
        image.setImageBitmap(map);

        
        
        
        //Bitmap map = new Bitmap(memStream);
        return image;
    }
    
    public void SaveImage(ImageView image) throws FileNotFoundException
    {
    	
        File file;
        file= new File (get_FullPath());  


        if (file.exists())
            return;
      //  Bitmap myBitmap = BitmapFactory.decodeFile(get_FullPath());
      //  image.setImageBitmap(image);
        Bitmap bitmap=image.getDrawingCache();;
     //  bitmap = Bitmap.(image);
     //   File file = new File(extStorageDirectory, image);
        		//bitmap.compress(Bitmap.CompressFormat.PNG, 90, image);
        FileOutputStream out = new FileOutputStream(get_FullPath());
       bitmap.compress(Bitmap.CompressFormat.PNG,100,out);
     //   bitmap.Dispose();
        
    }

    public byte[] ConvertToByte(ImageView image)
    {
    	/*
        Bitmap map = new Bitmap(image);
        MemoryStream ms = new MemoryStream();
        map.Save(ms, ImageFormat.Jpeg);
        byte[] byteResult = ms.ToArray();
        ms.Close();
        map.Dispose();
        return byteResult;
        */
    	
    	// Bitmap bitmap=BitmapFactory.decodeResource(getResources(),image );
    	 Bitmap bitmap=image.getDrawingCache();;
    	    ByteArrayOutputStream stream=new ByteArrayOutputStream();
    	    bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
    	    byte[] image2=stream.toByteArray();
    	    System.out.println("byte array:"+image2);

    	    String img_str = Base64.encodeToString(image2, 0);
    	    System.out.println("string:"+img_str);
    	return image2;
    }

    public byte[] ConvertToByte() throws FileNotFoundException
    {
       /*
         Bitmap map = new Bitmap(FullPath);
        
        MemoryStream ms = new MemoryStream();
        map.Save(ms, ImageFormat.Png);
        byte[] byteResult = ms.ToArray();
        ms.Close();
        map.Dispose();
        return byteResult;
        */
    	byte[] data ;
    	String filepath = get_FullPath();
    	File imagefile = new File(filepath + "icon.png");
    	FileInputStream fis ;
    	
    	fis= new FileInputStream(imagefile);;
	
		Bitmap bi = BitmapFactory.decodeStream(fis);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
	    	bi.compress(Bitmap.CompressFormat.PNG, 100, baos);
    	data= baos.toByteArray();
    	return data;

    }

    public boolean Exist()
    {
        File file;
        file= new File (get_FullPath()); 
        return file.exists();
    }

}

